package cn.agiledata.bank.account.facade;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import cn.agiledata.bank.account.dao.IAccountDAO;
import cn.agiledata.bank.account.service.AccountSummary;
import cn.agiledata.bank.account.service.IAccountConst;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.SecurityUtil;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.OperSubacct;
import cn.agiledata.bank.hibernateBean.Operator;
import cn.agiledata.bank.hibernateBean.SubAccount;

public class AccountFacadeImpl implements AccountFacade {
    private IAccountDAO accountDao;

    public void setAccountDao(IAccountDAO accountDao) {
        this.accountDao = accountDao;
    }

    public Account getAccountById(long id) {
        return this.accountDao.getAccountById(new Long(id));
    }

    public Account currentAccount(HttpServletRequest request) {
        long accountId = WeatherUtil.getAccountIdFromSession(request);
        return getAccountById(accountId);
    }

    public List getSubAccounts(HttpServletRequest request, String permissionType) {
        AccountSummary user = WeatherUtil.getLoingInfoFromSession(request);
        List subAccounts = new ArrayList();
        Long operatorCode = user.getOperatorCode();
        if (operatorCode == null) { // 操作员为空即个人用户，返回所有子账户
            Long accountId = new Long(user.getAccountId());
            Account account = this.accountDao.getAccountById(accountId);
            for (Iterator it = account.getSubAccounts().iterator(); it
                    .hasNext();) {
                subAccounts.add(it.next());
            }
        } else { // 操作员不为空，则只返回具有交易权限的子账户
            Operator operator = user.getOperatorByIdInstance(operatorCode
                    .toString());
            for (Iterator it = operator.getOperSubaccts().iterator(); it
                    .hasNext();) {
                OperSubacct os = (OperSubacct) it.next();
                Long listSeq = os.getId().getSysbook().getListSeq();
                if (listSeq.toString().equals(permissionType)) {
                    subAccounts.add(os.getId().getSubAccount());
                }
            }
        }
        return subAccounts;
    }

    public boolean hasPermission(HttpServletRequest request,
            String permissionType) {
        // 企业管理员没有交易权限
        if (isEnterpriseAdmin(request)) {
            return false;
        }
        if (isEnterpriseOperator(request)) { // 企业操作员
            AccountSummary user = WeatherUtil.getLoingInfoFromSession(request);
            Operator operator = user.getOperatorByIdInstance(user
                    .getOperatorCode().toString());
            for (Iterator it = operator.getOperSubaccts().iterator(); it
                    .hasNext();) {
                OperSubacct os = (OperSubacct) it.next();
                Long listSeq = os.getId().getSysbook().getListSeq();
                if (listSeq.toString().equals(permissionType)) {
                    return true;
                }
            }
            return false;
        } else { // 个人用户具有所有交易权限
            return true;
        }
    }

    public boolean isEnterpriseAdmin(HttpServletRequest request) {
        AccountSummary user = WeatherUtil.getLoingInfoFromSession(request);
        return IAccountConst.USERTYPE_CORP_ADMIN.equals(user.getUserType());
    }

    public boolean isEnterpriseOperator(HttpServletRequest request) {
        AccountSummary user = WeatherUtil.getLoingInfoFromSession(request);
        return IAccountConst.USERTYPE_CORP_OPERATOR.equals(user.getUserType());
    }

    public boolean isEnterprise(Account account) {
        return Constant.ACCOUNT_TYPE_CORPORATION.equals(account
                .getAccountType());
    }

    public boolean isEnterprise(HttpServletRequest request) {
        AccountSummary user = WeatherUtil.getLoingInfoFromSession(request);
        String userType = user.getUserType();
        return IAccountConst.USERTYPE_CORP_ADMIN.equals(userType)
                || IAccountConst.USERTYPE_CORP_OPERATOR.equals(userType);
    }

    public boolean isPerson(Account account) {
        return Constant.ACCOUNT_TYPE_BUYER.equals(account.getAccountType());
    }

    public boolean isPerson(HttpServletRequest request) {
        AccountSummary user = WeatherUtil.getLoingInfoFromSession(request);
        return IAccountConst.USERTYPE_PERSONAL.equals(user.getUserType());
    }

    public SubAccount getSubAccountById(long id) {
        return this.accountDao.getSubAccountById(new Long(id));
    }

    public boolean isCorrectPayPassword(HttpServletRequest request,
            String payPassword) {
        if (isPerson(request)) {
            Account account = currentAccount(request);
            return SecurityUtil.encryptoMD5String(payPassword).equals(
                    account.getPayPassword());
        } else if (isEnterpriseOperator(request)) {
            Operator operator = WeatherUtil.getOperator(request);
            return SecurityUtil.encryptoMD5String(payPassword).equals(
                    operator.getPayPassword());
        } else
            return false;
    }
}
